package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.ActivityTag;
import com.example.campusforumapi.entity.Category;
import com.example.campusforumapi.service.ActivityTagService;
import com.example.campusforumapi.service.CategoryService;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activitytags")
public class ActivityTagController extends BaseController{
    @Autowired
    private ActivityTagService activityTagService;
    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
    ){
        //查询并封装分页结果集
        PageResult tagPage = activityTagService.getTagPage(page,pageSize);
        return Result.success(tagPage);
    }

    //    删除一条记录
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        activityTagService.delTag(ids);
        return Result.success();
    }

    //    新增一条记录
    @PostMapping ("/insert")
    public Result insert(@RequestBody ActivityTag activityTag){
        if(StringUtils.isEmpty(activityTag.getTagId()) || StringUtils.isEmpty(activityTag.getTagName())){
            return Result.error("分类名称名及分类id不能为空");
        }
        else if (activityTagService.findsameTagName(activityTag.getTagName())>=1) {
            return Result.error("活动分类名称已存在");
        }
        else if(activityTagService.findsameTagId(activityTag.getTagId())>=1){
            return Result.error("该活动分类ID已存在");
        }
        else if(activityTagService.insertTag(activityTag) > 0){
            return Result.success("插入成功");
        }else{
            return Result.error("插入异常");
        }

    }

    //    修改一条记录
    @PutMapping("/update")
    public Result update(@RequestBody ActivityTag activityTag){
        if(activityTagService.updTag(activityTag) > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败，该分类不存在");
        }

    }
}
